import java.util.Scanner;
 
public class Matrix {
  
  public static double[][] sum2DArrays(double[][] array1, double[][] array2) {
     int row = array1.length;
     int col = array1[0].length;
     double[][] array = new double[row][col];
 
     for (int i=0; i<row; i++) 
      for (int j=0; j<col; j++) 
       array[i][j] = array1[i][j] + array2[i][j];
      
      return array;
  }
  
  public static double[][] read2DArray(int row, int col){
     double[][] array = new double[row][col];
 
     Scanner scanner = new Scanner(System.in);
     System.out.println("Enter a "+row+" by "+col+" matrix row-wise\n");
     for (int i=0; i<row; i++) {
       for (int j=0; j<col; j++){ 
           System.out.printf("Enter element array[%d][%d]: ", i, j);
           array[i][j] = scanner.nextDouble();
       }
       System.out.println();
     } 
      return array;
  }
  
  public static void print2DArray(double[][] array) {
     int row = array.length;
     int col = array[0].length;
 
     for (int i=0; i<row; i++) {
      for (int j=0; j<col; j++) 
         System.out.print(array[i][j]+ "\t");
      System.out.println();
     } 
  }
  
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int row, column;
    System.out.print("Enter number of rows: ");
    row = scanner.nextInt();
    System.out.print("Enter number of columns: ");
    column = scanner.nextInt();
 
    double[][] array1 = read2DArray(row, column);
    double[][] array2 = read2DArray(row, column);
    double[][] array = sum2DArrays(array1, array2);
    System.out.println("array1 is: ");
    print2DArray(array1);
    System.out.println("array2 is: ");
    print2DArray(array2);
    System.out.println("The sum of the two arrays is: ");
    print2DArray(array);
  }
}
